package com.liuyk.commonlib.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.liuyk.commonlib.R;
import com.liuyk.commonlib.widget.MultiStateView;

/**
 * 带有加载状态的activity
 * <p>
 * Created by liuyk on 2019/10/16.
 */
public abstract class BaseLoadingActivity extends BaseActivity {

    private MultiStateView mMultiStateView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.base_loading_activity_layout);

        initView();
    }

    private void initView() {
        mMultiStateView = findViewById(R.id.multi_state_view);
        mMultiStateView.setViewState(MultiStateView.ViewState.LOADING);
        //noinspection ConstantConditions
        mMultiStateView.getView(MultiStateView.ViewState.ERROR).setOnClickListener(this);
    }

    protected void setLoadingContent(int resId) {
        //第三个参数设置为了true，表示将第一个参数所指定的布局添加到第二个参数的View中,为false的话，表示不将第一个参数所指定的View添加到root中
        //当root为null表示我不需要将第一个参数所指定的布局添加到任何容器中，同时也表示没有任何容器来来协助第一个参数所指定布局的根节点生成布局参数
        //此时需要把第三个参数设置成false，然后我们手动将resId添加到root中
        final View contentView = LayoutInflater.from(this).inflate(resId, mMultiStateView, false);
        setLoadingContent(contentView);
    }

    protected void setLoadingContent(View view) {
        mMultiStateView.setViewForState(view, MultiStateView.ViewState.CONTENT);
    }

    protected void showLoadingView() {
        mMultiStateView.setViewState(MultiStateView.ViewState.LOADING);
    }

    protected void showContentView() {
        mMultiStateView.setViewState(MultiStateView.ViewState.CONTENT);
    }

    protected void showErrorView() {
        mMultiStateView.setViewState(MultiStateView.ViewState.ERROR);
    }

    protected void showEmptyView() {
        mMultiStateView.setViewState(MultiStateView.ViewState.EMPTY);
    }

    protected MultiStateView.ViewState getCurrentViewSate() {
        return mMultiStateView.getViewState();
    }

    protected void setEmptyView(int resId) {
        View emptyView = LayoutInflater.from(this).inflate(resId, mMultiStateView, false);
        setEmptyView(emptyView);
    }

    protected void setEmptyView(View emptyView) {
        mMultiStateView.setViewForState(emptyView, MultiStateView.ViewState.EMPTY);
    }

    protected View getEmptyView() {
        return mMultiStateView.getView(MultiStateView.ViewState.EMPTY);
    }

    protected void setFailView(int resId) {
        View failView = LayoutInflater.from(this).inflate(resId, mMultiStateView, false);
        setFailView(failView);
    }

    protected void setFailView(View failView) {
        mMultiStateView.setViewForState(failView, MultiStateView.ViewState.ERROR);
    }

    protected View getFailView() {
        return mMultiStateView.getView(MultiStateView.ViewState.ERROR);
    }

    protected void setLoadingView(int resId) {
        View loadingView = LayoutInflater.from(this).inflate(resId, mMultiStateView, false);
        setLoadingView(loadingView);
    }

    protected void setLoadingView(View loadingView) {
        mMultiStateView.setViewForState(loadingView, MultiStateView.ViewState.LOADING);
    }

    protected View getLoadingView() {
        return mMultiStateView.getView(MultiStateView.ViewState.LOADING);
    }

    protected abstract void onRetry();

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mMultiStateView.getView(MultiStateView.ViewState.ERROR)) {
            showLoadingView();
            onRetry();
        }
    }

}
